﻿package com.tvein.sanguo.swc.action.command
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	import fl.events.SliderEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Command4 extends MovieClip
	{
		public static const NAME:String = "Command-4";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var saleList:DataGrid;
		private var buySlider:Slider;
		
		private var roleMoney:int;
		private var goodsID:int;
		private var unit:String;
		private var unitPrice:int;
		private var maxCount:int;
		
		public function Command4()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtGoods.border = true;
			txtGoods.borderColor = 0x333333;
			
			saleList = new DataGrid();
			saleList.x = 5;
			saleList.y = 70;
			saleList.width = 590;
			saleList.height = 110;
			this.addChild(saleList);
			
			buySlider = new Slider();
			buySlider.x = 250;
			buySlider.y = 330;
			buySlider.width = 240;
			buySlider.height = 3;
			buySlider.minimum = 0;
			buySlider.snapInterval = 1;
			buySlider.tickInterval = 10;
			buySlider.enabled = true;
			this.addChild(buySlider);
			
			btnBuy.visible = false;
			btnBuyMinus.visible = false;
			btnBuyPlus.visible = false;
			
			buySlider.addEventListener(SliderEvent.CHANGE, onChange);
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnBuy.addEventListener(MouseEvent.CLICK, onClick);
			btnBuyMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnBuyPlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					reRoad();
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			saleList.setRendererStyle("textFormat", style);
			saleList.addEventListener(ListEvent.ITEM_CLICK, onSelected);
			
			var c0:DataGridColumn = new DataGridColumn("ID");
			var c1:DataGridColumn = new DataGridColumn("名称");
			var c2:DataGridColumn = new DataGridColumn("单位");
			var c3:DataGridColumn = new DataGridColumn("负重");
			var c4:DataGridColumn = new DataGridColumn("单价");
			var c5:DataGridColumn = new DataGridColumn("最大可售");
			
			c0.visible = false;
			c1.width = 190;
			c2.width = 100;
			c3.width = 100;
			c4.width = 100;
			c5.width = 100;
			
			saleList.addColumn(c0);
			saleList.addColumn(c1);
			saleList.addColumn(c2);
			saleList.addColumn(c3);
			saleList.addColumn(c4);
			saleList.addColumn(c5);
		
			for (var i:int = 0; i < params.length; i++)
			{
				if (params[i].GoodsID)
				{
					saleList.addItem({"ID":params[i].GoodsID, "名称":params[i].GoodsName, "单位":params[i].Unit, "负重":params[i].Burden, "单价":params[i].UnitPrice, "最大可售":params[i].StockNum});
				}
				
				if (params[i].RoleMoney)
				{
					roleMoney = params[i].RoleMoney;
				}
			}
		}
		
		private function reRoad():void
		{
			if (buySlider.value == 0)
			{
				txtBuy.text = "";
				btnBuyMinus.visible = false;
				
				btnBuy.visible = false;
			}
			else
			{
				txtBuy.text = buySlider.value + " " + unit;
				btnBuyMinus.visible = true;
				
				btnBuy.visible = true;
			}
			
			if (buySlider.value < maxCount)
			{
				btnBuyPlus.visible = true;
			}
			else
			{
				btnBuyPlus.visible = false;
			}
		}
		
		private function onSelected(evt:ListEvent):void
		{
			goodsID = evt.item["ID"];
			maxCount = evt.item["最大可售"];	
			unit = evt.item["单位"];	
			unitPrice = evt.item["单价"];
			
			if (maxCount > (roleMoney / unitPrice)) 
			{
				maxCount = (roleMoney / unitPrice);
			}
			
			txtGoods.text = evt.item["名称"];
			
			buySlider.maximum = maxCount;
			buySlider.value = 0;
			
			reRoad();
		}
		
		private function onChange(evt:SliderEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			reRoad();
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Command-4"]));
					break;
				case "btnBuy":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["CommandCall(4,CommandUse," + targetID + ",goodsId=" + goodsID + "&count=" + buySlider.value + ");"]]));
					break;
				case "btnBuyMinus":
					buySlider.value -= 1;
					reRoad();
					break;
				case "btnBuyPlus":
					buySlider.value += 1;
					reRoad();
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			buySlider.removeEventListener(SliderEvent.CHANGE, onChange);
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnBuy.removeEventListener(MouseEvent.CLICK, onClick);
			btnBuyMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnBuyPlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}